﻿using System.Linq;
using UniMana.Utils.Controls;
using System.Data;
using UniMana.Utils.Data;
using System;
namespace UniMana.Data
{
    public class Training_Subject_ClassInfo
    {
        //private DBScore m_data = new DBScore();
        #region /*Không thay đổi*/
            public bds m_bds = new bds();
            public void Save()
            {
                m_bds.EndEdit();
                m_data.m_DCSave.SubmitChanges();
            }
            public void Delete()
            {
                m_bds.DeleteCurrent();
            }
            #endregion /*Không thay đổi*/
            private DBMain m_data = new DBMain();
        #region [Properties - Thuộc tính]
       
        public Training_Subject_Class Properties
        {
            get { return m_bds.Current as Training_Subject_Class; }
        }
        #endregion

        public Training_Subject_ClassInfo(int subjectClassKey = 0)
        {
            var IQuery = m_data.m_DCSave.Training_Subject_Classes.Where(tb => tb.Training_Subject_ClassKey == subjectClassKey );
            m_bds.DataSource = IQuery;
            var Row = IQuery.FirstOrDefault();
            if (Row == null)
            {

                Row = new Training_Subject_Class();
                m_bds.Add(Row);
            }
        }

        public IQueryable GetByKey(int subjectClassKey)
        {
            var l = from p in m_data.m_DCReadOnly.Training_Subject_Classes
                    where p.Training_Subject_ClassKey == subjectClassKey
                    select p;
            return l;
        }

        public Training_Subject_ClassInfo(string _strID,bool getnum = false)
        {
            var iQuery = this.m_data.m_DCSave.Training_Subject_Classes.Where(p => p.ID == _strID);
            this.m_bds.DataSource = iQuery;
            var row = iQuery.FirstOrDefault();
            this.m_bExits = true;
            if (row == null)
            {
                this.m_bExits = false;
                row = new Training_Subject_Class();
                this.m_bds.Add(row);
            }
            if (m_bExits)
            {
                if (getnum)
                {
                    string[] str = _strID.Split('_');
                    _strID = str[0] + "_" + str[1] + "_" + str[2] + "_" + str[3];
                    iQuery = from kk in m_data.m_DCSave.Training_Subject_Classes
                             where kk.ID.StartsWith(_strID)
                             select kk;
                             
                        //this.m_data.m_DCSave.Training_Subject_Classes.Contains(ParallelEnumerable 
                    DataTable dt = iQuery.OrderByDescending(p => p.ID).Take(1).ToDataTable();
                    if (dt.Rows.Count > 0)
                    {
                        str = dt.Rows[0]["ID"].ToString().Split('_');
                        int count = -1;
                        if (str.Count() > 4)
                        {
                            _strID = str[str.Count() - 1];
                            if (_strID != "")
                            {
                                try
                                {
                                    count = Convert.ToInt16(_strID);
                                }
                                catch
                                {
                                }
                            }
                        }
                        this.NumSJC = count;
                    }
                }
            }
        }

        public IQueryable TotalCredits(int subjectClass)
        {
            var credits = from sc in m_data.m_DCReadOnly.Training_Subject_Classes
                          join
                              s in m_data.m_DCReadOnly.Training_Subjects on sc.Training_SubjectKey equals s.Training_SubjectKey
                          where sc.Training_Subject_ClassKey == subjectClass
                          select new
                          {
                              sc.Training_Subject_ClassKey,
                              sc.Training_SubjectKey,
                              TotalCredits = s.TotalCredits == null ? 0 : s.TotalCredits
                          };
            return credits;
        }

        #region Chi --Hòa
        private bool m_bExits = false;
        public bool ExistInDatabase
        {
            get { return this.m_bExits; }
        }

        private int NumSJC = -1;
        public int NumSjC
        {
            get { return this.NumSJC; }
        }
        #endregion
    }
}
